package net.simflow.swf.analyzer.network.data;

import java.net.URI;
import java.net.URISyntaxException;

import uk.org.taverna.scufl2.api.core.Processor;
import uk.org.taverna.scufl2.api.core.Workflow;
import uk.org.taverna.scufl2.api.port.InputWorkflowPort;
import uk.org.taverna.scufl2.api.port.OutputWorkflowPort;

public class Instance {

	private String name;
	private URI type;
	private URI wsdlURI;
	private String wsdlOperation;
	private String flowName;
	private URI flowURI;

	private Instance(String name, URI type, URI wsdlURI, String wsdlOperation,
			String flowName, URI flowURI) {
		this.name = name;
		this.type = type;
		this.wsdlURI = wsdlURI;
		this.wsdlOperation = wsdlOperation;
		this.flowName = flowName;
		this.flowURI = flowURI;
	}

	public static Instance makeInstance(Processor processor,
			ProcessorConfig config, Workflow workflow) {
		String name;
		URI type = null;
		URI wsdlURI = null;
		String wsdlOperation = null;
		String flowName;
		URI flowURI;

		name = processor.getName();
		flowName = workflow.getName();
		flowURI = workflow.getWorkflowIdentifier();

		if (config != null) {
			if (config.getActivity() != null) {
				type = config.getActivity().getConfigurableType();
			}

			if (config.getConfiguration() != null) {
				try {
					if (type.equals(new URI(
							"http://ns.taverna.org.uk/2010/activity/wsdl"))) {
						wsdlOperation = config
								.getConfiguration()
								.getPropertyResource()
								.getPropertyAsResource(
										new URI(
												"http://ns.taverna.org.uk/2010/activity/wsdl#operation"))
								.getPropertyAsString(
										new URI(
												"http://ns.taverna.org.uk/2010/activity/wsdl/operation#name"));
						wsdlURI = config
								.getConfiguration()
								.getPropertyResource()
								.getPropertyAsResource(
										new URI(
												"http://ns.taverna.org.uk/2010/activity/wsdl#operation"))
								.getPropertyAsReference(
										new URI(
												"http://ns.taverna.org.uk/2010/activity/wsdl/operation#wsdl"))
								.getResourceURI();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}

		return new Instance(name, type, wsdlURI, wsdlOperation, flowName,
				flowURI);
	}

	public static Instance makeInstance(InputWorkflowPort port,
			Workflow workflow) {
		String name;
		URI type = null;
		URI wsdlURI = null;
		String wsdlOperation = null;
		String flowName;
		URI flowURI;

		name = port.getName();
		try {
			type = new URI("net.simflow.swf.inputport");
		} catch (URISyntaxException e) {

		}
		flowName = workflow.getName();
		flowURI = workflow.getWorkflowIdentifier();

		return new Instance(name, type, wsdlURI, wsdlOperation, flowName,
				flowURI);
	}

	public static Instance makeInstance(OutputWorkflowPort port,
			Workflow workflow) {
		String name;
		URI type = null;
		URI wsdlURI = null;
		String wsdlOperation = null;
		String flowName;
		URI flowURI;

		name = port.getName();
		try {
			type = new URI("net.simflow.swf.outputport");
		} catch (URISyntaxException e) {

		}
		flowName = workflow.getName();
		flowURI = workflow.getWorkflowIdentifier();

		return new Instance(name, type, wsdlURI, wsdlOperation, flowName,
				flowURI);
	}

	public String getName() {
		return name;
	}

	public URI getType() {
		return type;
	}

	public String getWSDLOperation() {
		return wsdlOperation;
	}

	public URI getWSDLURI() {
		return wsdlURI;
	}

	public String getFlowName() {
		return flowName;
	}

	public URI getFlowURI() {
		return flowURI;
	}

	public String getShortInfo() {
		return this.getName();
	}

	public String getFullInfo() {

		StringBuffer str = new StringBuffer();

		str.append(this.getName());

		StringBuffer flowuri = new StringBuffer(getFlowURI().getPath());
		flowuri.deleteCharAt(flowuri.length() - 1);
		String flowid = flowuri.substring(flowuri.lastIndexOf("/") + 1,
				flowuri.length());
		str.append("(" + getFlowName() + "\"" + flowid + "\"" + ")");

		return str.toString();
	}

	@Override
	public String toString() {
		return getFullInfo();
	}
}
